﻿using Hidistro.Core;
using Hidistro.Membership.Context;
using JSShop.Model;
using Juse.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class ProductView : Control
    {
        private vw_Hishop_BrowseProductList product;
        private string Template;
        protected override void OnDataBinding(EventArgs e)
        {
            this.product = this.Page.GetDataItem() as vw_Hishop_BrowseProductList;
            if (this.product != null)
            {
                this.Template = File.ReadAllText(this.MapPathSecure(HiContext.Current.GetSkinPath() + this.SkinPath));
                if (!string.IsNullOrEmpty(this.Template))
                {
                    this.Template = Regex.Replace(this.Template, @"<!--[\s\S]*?-->", "");
                    string url = Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", this.product.ProductId, this.product.CategoryUrl);
                    this.Template = this.Template.Replace("{Url}", url);
                    this.Template = this.Template.Replace("{Image}", Globals.GetImgUrl(this.product.ThumbnailUrl310));
                    this.Template = Regex.Replace(this.Template, @"\{Promotions\|(((?:\{\w+\})|[\s\S])*?)\}", this.product.CountDownId != null ? "$1" : "");
                    if (this.product.CountDownId != null)
                    {
                        this.Template = this.Template.Replace("{TimeRemaining}", (this.product.EndDate.Value.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds.ToString());
                    }
                    this.Template = Regex.Replace(this.Template, @"\{Freight\|(((?:\{\w+\})|[\s\S])*?)\}", this.product.IsFreight ? "$1" : "");
                    foreach (PropertyInfo item in typeof(vw_Hishop_BrowseProductList).GetProperties())
                    {
                        object val = item.GetValue(this.product, null);
                        string str = "";
                        if (val != null)
                        {
                            if (item.PropertyType == typeof(decimal) || item.PropertyType == typeof(decimal?))
                            {
                                str = ((decimal)val).ToString("F2");
                            }
                            else
                            {
                                str = val.ToString();
                            }
                        }
                        this.Template = this.Template.Replace("{" + item.Name + "}", str);
                    }
                }
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.Template);
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinPath == null)
            {
                this.SkinPath = "/ascx/tags/Common_ViewProduct/ProductView.html";
            }
            base.OnInit(e);
        }
        public string SkinPath
        {
            get;
            set;
        }
    }
}
